package com.pai4j.pgc.controller;

import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.domain.vo.request.creator.ApplyCreatorRequestVO;
import com.pai4j.domain.vo.request.creator.AuditCreatorRequestVO;
import com.pai4j.domain.vo.request.creator.UpdateCreatorRequestVO;
import com.pai4j.domain.vo.response.creator.CreatorAuditRecordResponseVO;
import com.pai4j.domain.vo.response.creator.CreatorBaseResponseVO;
import com.pai4j.domain.vo.response.creator.CreatorDetailResponseVO;
import com.pai4j.domain.vo.response.creator.CreatorStatisticsResponseVO;
import com.pai4j.log.annotation.OperationLog;
import com.pai4j.pgc.service.CreatorService;
import com.pai4j.remote.pgc.CreatorClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 创作者控制器
 * 提供创作者管理的REST API接口
 *
 * @author: CYM-pai
 * @date: 2025/11/01
 */
@Slf4j
@RestController
public class CreatorController implements CreatorClient {
    
    @Resource
    private CreatorService creatorService;
    
    /**
     * 申请成为创作者
     */
    @Override
    @OperationLog(
        module = "创作者管理",
        operation = "申请创作者",
        description = "用户申请成为创作者",
        recordParams = true
    )
    public PAIResponseBean<Boolean> applyCreator(ApplyCreatorRequestVO request) {
        try {
            Boolean result = creatorService.applyCreator(request);
            return PAIResponseBeanUtil.success(result);
        } catch (Exception e) {
            log.error("申请创作者失败: userId={}, error={}", request.getUserId(), e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 获取创作者详情
     */
    @Override
    public PAIResponseBean<CreatorDetailResponseVO> getCreatorInfo(String userId) {
        try {
            CreatorDetailResponseVO detail = creatorService.getCreatorByUserId(userId);
            return PAIResponseBeanUtil.success(detail);
        } catch (Exception e) {
            log.error("获取创作者详情失败: userId={}, error={}", userId, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 更新创作者信息
     */
    @Override
    @OperationLog(
        module = "创作者管理",
        operation = "更新创作者信息",
        description = "更新创作者个人信息",
        recordParams = true
    )
    public PAIResponseBean<Boolean> updateCreator(UpdateCreatorRequestVO request) {
        try {
            Boolean result = creatorService.updateCreator(request);
            return PAIResponseBeanUtil.success(result);
        } catch (Exception e) {
            log.error("更新创作者信息失败: userId={}, error={}", request.getUserId(), e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 审核创作者申请（管理员）
     */
    @Override
    @OperationLog(
        module = "创作者管理",
        operation = "审核创作者",
        description = "管理员审核创作者申请",
        recordParams = true
    )
    public PAIResponseBean<Boolean> auditCreator(AuditCreatorRequestVO request) {
        try {
            Boolean result = creatorService.manualAudit(request);
            return PAIResponseBeanUtil.success(result);
        } catch (Exception e) {
            log.error("审核创作者失败: creatorId={}, error={}", request.getCreatorId(), e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 分页查询创作者（按状态）
     */
    @Override
    public PAIResponseBean<PAIPageResponseBeanUtil<List<CreatorBaseResponseVO>>> pageCreators(
            Integer status, Integer pageNo, Integer pageSize) {
        try {
            PAIPageResponseBeanUtil<List<CreatorBaseResponseVO>> result = 
                    creatorService.pageCreatorsByStatus(status, pageNo, pageSize);
            return PAIResponseBeanUtil.success(result);
        } catch (Exception e) {
            log.error("分页查询创作者失败: status={}, error={}", status, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 获取创作者排行榜（按经验值）
     */
    @Override
    public PAIResponseBean<PAIPageResponseBeanUtil<List<CreatorBaseResponseVO>>> getCreatorRanking(
            Integer pageNo, Integer pageSize) {
        try {
            PAIPageResponseBeanUtil<List<CreatorBaseResponseVO>> result = 
                    creatorService.pageCreatorRanking(pageNo, pageSize);
            return PAIResponseBeanUtil.success(result);
        } catch (Exception e) {
            log.error("获取创作者排行榜失败: error={}", e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 禁用创作者
     */
    @Override
    @OperationLog(
        module = "创作者管理",
        operation = "禁用创作者",
        description = "管理员禁用创作者",
        recordParams = true
    )
    public PAIResponseBean<Boolean> disableCreator(String userId, String operator) {
        try {
            Boolean result = creatorService.disableCreator(userId, operator);
            return PAIResponseBeanUtil.success(result);
        } catch (Exception e) {
            log.error("禁用创作者失败: userId={}, error={}", userId, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 启用创作者
     */
    @Override
    @OperationLog(
        module = "创作者管理",
        operation = "启用创作者",
        description = "管理员启用创作者",
        recordParams = true
    )
    public PAIResponseBean<Boolean> enableCreator(String userId, String operator) {
        try {
            Boolean result = creatorService.enableCreator(userId, operator);
            return PAIResponseBeanUtil.success(result);
        } catch (Exception e) {
            log.error("启用创作者失败: userId={}, error={}", userId, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 获取创作者审核记录
     */
    @Override
    public PAIResponseBean<List<CreatorAuditRecordResponseVO>> getAuditRecords(String userId) {
        try {
            List<CreatorAuditRecordResponseVO> records = creatorService.getAuditRecords(userId);
            return PAIResponseBeanUtil.success(records);
        } catch (Exception e) {
            log.error("获取审核记录失败: userId={}, error={}", userId, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 获取创作者统计信息
     */
    @Override
    public PAIResponseBean<CreatorStatisticsResponseVO> getCreatorStatistics(String userId) {
        try {
            CreatorStatisticsResponseVO statistics = creatorService.getCreatorStatistics(userId);
            return PAIResponseBeanUtil.success(statistics);
        } catch (Exception e) {
            log.error("获取创作者统计信息失败: userId={}, error={}", userId, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 增加创作者经验值
     */
    @Override
    public PAIResponseBean<Boolean> addExperience(String userId, Integer points) {
        try {
            creatorService.addExperience(userId, points);
            return PAIResponseBeanUtil.success(true);
        } catch (Exception e) {
            log.error("增加经验值失败: userId={}, points={}, error={}", userId, points, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 更新创作者信用分
     */
    @Override
    public PAIResponseBean<Boolean> updateCreditScore(String userId, Integer scoreDelta) {
        try {
            creatorService.updateCreditScore(userId, scoreDelta);
            return PAIResponseBeanUtil.success(true);
        } catch (Exception e) {
            log.error("更新信用分失败: userId={}, scoreDelta={}, error={}", userId, scoreDelta, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 增加文章数
     */
    @Override
    public PAIResponseBean<Boolean> increaseArticleCount(String userId) {
        try {
            creatorService.increaseArticleCount(userId);
            return PAIResponseBeanUtil.success(true);
        } catch (Exception e) {
            log.error("增加文章数失败: userId={}, error={}", userId, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 增加粉丝数
     */
    @Override
    public PAIResponseBean<Boolean> increaseFollowerCount(String userId, Integer count) {
        try {
            creatorService.increaseFollowerCount(userId, count);
            return PAIResponseBeanUtil.success(true);
        } catch (Exception e) {
            log.error("增加粉丝数失败: userId={}, count={}, error={}", userId, count, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
    
    /**
     * 增加收益
     */
    @Override
    public PAIResponseBean<Boolean> addIncome(String userId, Long amount) {
        try {
            creatorService.addIncome(userId, amount);
            return PAIResponseBeanUtil.success(true);
        } catch (Exception e) {
            log.error("增加收益失败: userId={}, amount={}, error={}", userId, amount, e.getMessage(), e);
            return PAIResponseBeanUtil.fail(e.getMessage());
        }
    }
}

